﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<TogheterWeBuyIt.Models.Promocao>>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
	Index
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Promoções</h2> <br />
    <% Html.BeginForm("Index", "Promocao", FormMethod.Post); %>
          <table align="center" width="100%" border="0" style="border-width:0; border-style:none;"><tr><td>Pesquisar por:</td><td  style="width:73%;"><input type="text" name="TxbBusca" style="width:100%;" /></td><td><input type="submit" value="Pesquisar" /></td></tr></table>
    <% Html.EndForm(); %>
          <br />
    <table align="center" width="100%">
        <tr>
            <th>
               Criada por
            </th>
            <th>
                Descrição
            </th>
            <th>
                Data início
            </th>
            <th>
                Data término
            </th>
            <th>
                Mín/Máx pessoas
            </th>
            <th>
                Link
            </th>
            <th>
                Participantes
            </th>
            <th></th>
            <th></th>
        </tr>

    <% foreach (var item in Model) { %>
    
        <tr>
            <td>
                <%= Html.ActionLink(item.Usuario.Login, "Details", "Usuario", new { id = item.IdUsuario }, null)%>
            </td>
            <td>
                <%= Html.Encode(item.Descricao) %>
            </td>
            <td>
                <%= Html.Encode(!item.DataValidadeInicio.HasValue ? "Indefinido" : item.DataValidadeInicio.Value.ToString("dd/MM/yyyy")) %>
            </td>
            <td>
                <%= Html.Encode(!item.DataValidadeInicio.HasValue ? "Indefinido" : item.DataValidadeFim.Value.ToString("dd/MM/yyyy"))%>
            </td>
            <td>
                <%= Html.Encode(item.ClientesMin) %> / <%= Html.Encode(item.ClientesMax) %>
            </td>
            <td>
                <%= Html.Encode(item.Link) %>
            </td>
            <td>
                <%= item.ParticipaPromocaos != null ? Html.Encode(item.ParticipaPromocaos.Count) : "0" %>
            </td>
            <td>
                <%= Html.ActionLink("Visualizar", "Details", new { id=item.Id })%>
            </td>
            <td>
            <% if (Session["LoggedUserId"] == null)
               { %>
               <%= Html.ActionLink("LogIn", "LogIn", "Usuario",null, null) %>
            <% }
               else if (item.ParticipaPromocaos != null && !item.ParticipaPromocaos.Any(p => p.IdUsuario == (int)Session["LoggedUserId"]))
               { %>
                    <%= Html.ActionLink("Participar", "Participate", new { id = item.Id })%>
            <% }
               else
               { %>
                    <%= "Já estou participando"%>
            <% } %>
            </td>
        </tr>
    
    <% } %>

    </table>
    <p><br /></p>
    <p>
        <%= Html.ActionLink("Criar nova promoção", "Create") %>
    </p>

</asp:Content>

